package com.bfs.anasit.jsfbean;

import com.bfs.anasit.model.AnasitRole;
import com.bfs.anasit.model.AnasitUser;
import com.bfs.anasit.model.Patient;
import com.bfs.anasit.service.AnasitUserService;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: lukman
 * Date: 6/24/11
 * Time: 12:04 PM
 * To change this template use File | Settings | File Templates.
 */
@Named
@ConversationScoped
public class AnasitUserBean implements Serializable {
//    private static final Logger log = LoggerFactory.getLogger(AnasitUserBean.class);

    @Inject
    org.jboss.logging.Logger log;

    @Inject
    List<Patient> patientList;


    @Inject
    private AnasitUser anasitUser;
    private AnasitUser selectedAnasitUser;
    private List<AnasitRole> anasitRoles;

    private List<AnasitUser> anasitUserList;

    //IOC instances
    @Inject
    private AnasitUserService anasitUserService;

    public AnasitUserBean() {
    }

    public AnasitUser getAnasitUser() {
        return anasitUser;
    }

    public void setAnasitUser(AnasitUser anasitUser) {
        this.anasitUser = anasitUser;
    }


    public List<AnasitUser> getAnasitUserList() {
        anasitUserList = anasitUserService.findAllAnasitUsers();
        return anasitUserList;

    }

    public void setAnasitUserList(List<AnasitUser> anasitUserList) {
        this.anasitUserList = anasitUserList;
    }

    public AnasitUserService getAnasitUserService() {
        return anasitUserService;
    }

    public void setAnasitUserService(AnasitUserService anasitUserService) {
        this.anasitUserService = anasitUserService;
    }

    public AnasitUser getSelectedAnasitUser() {
        return selectedAnasitUser;
    }

    public void setSelectedAnasitUser(AnasitUser selectedAnasitUser) {
        this.selectedAnasitUser = selectedAnasitUser;
    }

    /**
     * set all fields empty after saving
     */
    public void clearAnasitUserProperties() {
        anasitUser.setId(null);
        anasitUser.getName().setFirstName("");
        anasitUser.getName().setMiddleName("");
        anasitUser.getName().setLastName("");
        anasitUser.setPassString("");
        anasitUser.setUserName("");
    }

    public void deleteUser(ActionEvent actionEvent) {
        // log.debug("About to delete user {} ", selectedAnasitUser.getUserName());
        anasitUserService.delete(selectedAnasitUser);
        //  log.debug("deleted user {} ", selectedAnasitUser.getUserName());

    }


    public String createAnasitUser() {
        anasitUser.getRole().setUser(anasitUser.getUserName());
        // log.debug("About to Create user with username {} ", anasitUser.getUserName());
        anasitUser = anasitUserService.save(anasitUser);
        //log.debug("Created user with username {} and redirecting", anasitUser.getUserName());
        //clearAnasitUserProperties();   "/user/users?faces-redirect=true"
        return "/user/view?faces-redirect=true&amp;includeViewParams=true";
    }

    public void onEditRow(RowEditEvent event) {
        AnasitUser selectedAnasitUser = (AnasitUser) event.getObject();
        anasitUserService.save(selectedAnasitUser);
    }

    public void onRowSelect(SelectEvent event) {
        selectedAnasitUser = (AnasitUser) event.getObject();


//        FacesMessage msg = new FacesMessage("User Selected", ((AnasitUser) event.getObject()).getUserName());
//        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String logoutUser() {
        // log.debug("About to Logout user {}", FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
        log.info("Logout user");
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        // log.debug("About to Logged out user {}", FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
        return "/home.xhtml?faces-redirect=true";
    }

    public String loadUser() {
        anasitUser = anasitUserService.findById(anasitUser.getId());
        return "/user/user?faces-redirect=true&amp;includeViewParams=true";
    }


    public List<AnasitRole> getAnasitRoles() {
        return anasitRoles;
    }

    public void setAnasitRoles(List<AnasitRole> anasitRoles) {
        this.anasitRoles = anasitRoles;
    }
}
